<?php
//setup user permission variables
  $fql='SELECT status_update,offline_access,photo_upload,create_event,rsvp_event,create_listing FROM permissions WHERE uid='.$fb_user.'';
    $perm_array=$facebook->api_client->fql_query($fql);
    $status_update=$perm_array[0]["status_update"];
    $_SESSION['status__update']=$status_update;
    $offline__access=$perm_array[0]["offline_access"];
    $_SESSION['offline__access']=$offline__access;
    $photo_upload=$perm_array[0]["photo_upload"];
    $_SESSION['photo_upload']=$photo_upload;
    $create_event=$perm_array[0]["create_event"];
    $_SESSION['create_event']=$create_event;
    $rsvp_event=$perm_array[0]["rsvp_event"];
    $_SESSION['rsvp_event']=$rsvp_event;
    $create_listing=$perm_array[0]["create_listing"];
    $_SESSION['create_listing']=$create_listing;
//setup user variables
 $fql='SELECT first_name,last_name FROM user WHERE uid='.$fb_user.'';
    $data = $facebook->api_client->fql_query($fql);
    $_SESSION['fb_user_firstname'] = $data[0]['first_name'];
    $_SESSION['fb_user_lastname'] = $data[0]['last_name']; 
//setup default user network variables
 $fql = 'SELECT affiliations FROM user WHERE uid='.$fb_user.'';
    $affiliations = $facebook->api_client->fql_query($fql);
    $_SESSION['fb_network_id'] = $affiliations[0]['affiliations'][0]['nid'];
     $_SESSION['fb_network_name'] = $affiliations[0]['affiliations'][0]['name'];

 //see if this is a new user
        $_SESSION['current_network_id']="";
         $sql = 'SELECT user_id, current_network_id from users where user_id='.$_SESSION['user'].';';
         $result = $db->Execute($sql);
         $fb_user_id = $result->fields['user_id'];
 //if not a user yet input user info
       $fb_network_id = $_SESSION['fb_network_id'];
       $fb_network_name = $_SESSION['fb_network_name'];
     if(!$fb_user_id)
     {
       $fb_user_id = $_SESSION['user'];
       $status__update = $_SESSION['status_update'];
       $fb_offline__access = $_SESSION['offline__access'];
       $photo_upload = $_SESSION['photo_upload'];
       $create_event = $_SESSION['create_event'];
       $rsvp_event = $_SESSION['rsvp_event'];
       $create_listing = $_SESSION['create_listing'];
       $sql2="INSERT into users values ( '$fb_user_id', '$fb_network_id', '$fb_network_id', '$status__update','$fb_offline__access','$photo_upload','$create_event','$rsvp_event','$create_listing')";
       $db->Execute($sql2);
        $_SESSION['current_network_id']=$fb_network_id;
     }
//if not a new user keep the current network id
    else
   {
        $_SESSION['current_network_id']=$result->fields['current_network_id'];
    }
//if the network doesn't exist, create it because facebook won't let us query network names directly
$sql='select network_name from networks where network_id='.$_SESSION['fb_network_id'].';';
$result=$db->Execute($sql);
if($result->fields['network_name'] == NULL)
 {
  $sql="insert into networks values ('$fb_network_id','$fb_network_name')";
  $db->Execute($sql);
 }

//store current network
  $sql = 'SELECT network_name FROM networks WHERE network_id='.$_SESSION['current_network_id'].'';
  $result= $db->Execute($sql);
  $_SESSION['current_network_name']=$result->fields['network_name'];
?>
